package org.infodavid.common.model.impl;

import org.infodavid.common.model.IGenericPropertiesMap;
import org.infodavid.common.model.IUserGroup;
import org.infodavid.common.model.ModelFactory;

/**
 * The Class GroupImpl.
 */
public class GroupImpl extends AbstractNamedCommented<Long> implements IUserGroup {

	/** serialVersionUID. */
	private static final long serialVersionUID = 6523279718688638430L;

	/** The locked. */
	private boolean locked;

	/** The default. */
	private boolean guest;

	/** The role. */
	private EUserRole role;

	/** The properties. */
	private IGenericPropertiesMap properties;

	/**
	 * constructor.
	 * @param keyArg the key
	 * @throws Exception the exception
	 */
	protected GroupImpl(final Long keyArg) throws Exception {
		super();

		setKey(keyArg);
	}

	/**
	 * constructor.
	 * @throws Exception the exception
	 */
	protected GroupImpl() throws Exception {
		super();
	}

	/**
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.IGenericObject#getProperties()
	 */
	public IGenericPropertiesMap getProperties() {
		return properties;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IBasicDataObject#init()
	 */
	@Override
	public void init() throws Exception {
		super.init();

		locked = false;
		guest = false;
		role = EUserRole.NORMAL;
		properties = ModelFactory.getInstance().create(IGenericPropertiesMap.class);
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.auth.model.IGroup#isLocked()
	 */
	public boolean isLocked() {
		return locked;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.IGroup#isGuest()
	 */
	public boolean isGuest() {
		return guest;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.IGroup#getRole()
	 */
	public EUserRole getRole() {
		return role;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.auth.model.IGroup#setLocked(boolean)
	 */
	@SuppressWarnings("boxing")
	public void setLocked(final boolean value) {
		if (!equals(locked, value)) {
			final Object old = locked;
			locked = value;
			markModified(LOCKED, old, locked);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.IGroup#setGuest(boolean)
	 */
	@SuppressWarnings("boxing")
	public void setGuest(final boolean value) {
		if (!equals(guest, value)) {
			final Object old = guest;
			guest = value;
			markModified(GUEST, old, guest);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.IGroup#setRole(org.infodavid.common.model.IGroup.EGroupRole)
	 */
	public void setRole(final EUserRole value) {
		if (!equals(role, value)) {
			final Object old = role;
			role = value;
			markModified(ROLE, old, role);
		}
	}

	/**
	 * Sets the properties.
	 * @param values the properties
	 */
	protected void setProperties(final IGenericPropertiesMap values) {
		final Object old = properties;
		properties = values;
		markModified(PROPERTIES, old, properties);
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#toString(java.lang.StringBuffer)
	 */
	@Override
	protected StringBuffer toString(final StringBuffer builder) {
		super.toString(builder);

		builder.append("[locked=");
		builder.append(locked);
		builder.append(",guest=");
		builder.append(guest);
		builder.append(",role=");
		builder.append(role);
		builder.append(",properties=");
		builder.append(properties);
		builder.append("]");

		return builder;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		final int result = 1;

		return prime * result + (getName() == null ? 0 : getName().hashCode());
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}

		if (!(obj instanceof IUserGroup)) {
			return false;
		}

		final IUserGroup other = (IUserGroup)obj;

		if (getName() == null) {
			if (other.getName() != null) {
				return false;
			}
		}
		else if (!getName().equals(other.getName())) {
			return false;
		}

		return true;
	}
}
